package com.bestcem.xm.ticket.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author gy.liu
 * @version 1.0
 * @date 2021/8/27 14:56
 **/
@Slf4j
@Component
public class CheckUtil {

    public static List<String> QTYPE = Arrays.asList("city", "cascader", "evaluation");

    public boolean wordMarker(Object answer, Map<String, List<Integer[]>> locs, String type) {
        if (type.equals("blank")) {
            // 空白处理
            if (answer instanceof List) {
                return markBlank((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("multiple_blank")) {
            // 多空白处理
            if (answer instanceof List) {
                return markMultiblank((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("single")) {
            // 单选处理
            if (answer instanceof List) {
                return markSingle((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("multiple")) {
            // 多选处理
            if (answer instanceof List) {
                return markMultiple((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("score")) {
            // 得分处理
            if (answer instanceof List) {
                return markScore((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("matrix_single")) {
            // 矩阵单选
            if (answer instanceof List) {
                return markMatrixSingle((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("matrix_multiple")) {
            // 矩阵多选
            if (answer instanceof List) {
                return markMatrixMultiple((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("city")) {
            // 城市
            //markCity((List<Object>) answer, locs);
            return answer instanceof List;
        } else if (type.equals("matrix_blank")) {
            // 矩阵空白
            if (answer instanceof List) {
                return markMatrixBlank((List<Map<String, Object>>) answer, locs);
            }
        } else if (type.equals("evaluation")) {
            // 评估
            if (answer instanceof Map) {
                return markEvaluationOpen((Map<String, Object>) answer, locs);
            }
        } else if (type.equals("sort")) {
            // 排序
            if (answer instanceof List) {
                return markSort((List<Map<String, Object>>) answer, locs);
            }
        }
        return false;
    }

    /**
     * 标记空白
     *
     * @param answer
     * @param innerData
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markBlank(List<Map<String, Object>> answer, Map<String, List<Integer[]>> innerData) {
        try {
            Map<String, Object> firstAnswer = answer.get(0);
            if (Objects.isNull(firstAnswer) || CollectionUtils.isEmpty(innerData)) {
                return false;
            }

            for (String key : innerData.keySet()) {
                if ("#0".equals(key)) {
                    if (answer.get(0).get("blank") instanceof List) {
                        // 重新设置blank
                        List<Map<String, Object>> blanks = (List<Map<String, Object>>) answer.get(0).get("blank");
                        if (blanks.get(0).get("v") instanceof String) {
                            String v = (String) blanks.get(0).get("v");
                            List<Map<String, Object>> mark = markWord(v, innerData.get(key));
                            answer.get(0).put("blank", mark);
                        }
                    } else if (answer.get(0).get("blank") instanceof String) {
                        String v = (String) answer.get(0).get("blank");
                        List<Map<String, Object>> mark = markWord(v, innerData.get(key));
                        answer.get(0).put("blank", mark);
                    }
                } else {
                    log.error("[Ticket] markBlank {} --> {}", key, innerData.get(key));
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            log.error("[Ticket] markBlank error", e);
            return false;
        }
    }

    /**
     * 标记多处空白
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markMultiblank(List<Map<String, Object>> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        try {
            Map<Integer, Object> seqAnsMap = new HashMap<>();
            if (Objects.nonNull(answers)) {
                for (Map<String, Object> answer : answers) {
                    seqAnsMap.put((Integer) answer.get("seq"), answer);
                }
            }
            for (String key : locs.keySet()) {
                Integer idx = 0;
                Integer seq = 0;
                if (key.contains("#")) {
                    String[] keys = key.split("#");
                    if (Objects.nonNull(keys) && keys.length > 1) {
                        idx = Integer.valueOf(keys[1]);
                        seq = Integer.valueOf(keys[0]);
                    } else if (keys.length == 1) {
                        seq = Integer.valueOf(keys[0]);
                    }
                } else {
                    seq = Integer.valueOf(key);
                }
                if (seqAnsMap.get(seq) instanceof Map) {
                    Map<String, Object> ans = (Map<String, Object>) seqAnsMap.get(seq);
                    // open类型处理
                    if (Integer.valueOf(0).equals(idx) && ans.containsKey("open")) {
                        if (ans.get("open") instanceof List) {
                            List<Map<String, Object>> openList = (List) ans.get("open");
                            if (openList.size() > 0) {
                                if (openList.get(0).get("v") instanceof String) {
                                    String v = (String) openList.get(0).get("v");
                                    ans.put("open", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            }
                        } else if (ans.get("open") instanceof String) {
                            String v = (String) ans.get("open");
                            ans.put("open", markWord(v, locs.get(key)));
                            mark = true;
                        }
                    }
                    // blank类型处理
                    if (Integer.valueOf(0).equals(idx) && !ans.containsKey("open")) {
                        if (ans.get("blank") instanceof List) {
                            List<Object> blankList = (List) ans.get("blank");
                            if (blankList.size() > 0) {
                                if (blankList.get(0) instanceof Map) {
                                    Map<String, Object> blanks = (Map<String, Object>) blankList;
                                    if (blanks.get("v") instanceof String) {
                                        String v = (String) blanks.get("v");
                                        ans.put("blank", markWord(v, locs.get(key)));
                                        mark = true;
                                    }
                                } else if (blankList.get(0) instanceof String) {
                                    String v = (String) blankList.get(0);
                                    ans.put("blank", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            }
                        } else if (ans.get("blank") instanceof String) {
                            String v = (String) ans.get("blank");
                            ans.put("blank", markWord(v, locs.get(key)));
                            mark = true;
                        }
                    } else if (Integer.valueOf(1).equals(idx)) {
                        if (ans.get("open") instanceof List) {
                            List<Map<String, Object>> openList = (List) ans.get("open");
                            if (openList.size() > 0) {
                                if (openList.get(0).get("v") instanceof String) {
                                    String v = (String) openList.get(0).get("v");
                                    ans.put("open", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            }
                        } else if (ans.get("open") instanceof String) {
                            String v = (String) ans.get("open");
                            ans.put("open", markWord(v, locs.get(key)));
                            mark = true;
                        }
                    } else {
                        log.error("markMultiblank {} --> {}", key, locs.get(key));
                    }
                }
            }
        } catch (Exception e) {
            log.error("markMultiblank error {}", e);
            return false;
        }
        return mark;
    }

    /**
     * 标记单选
     *
     * @param answer
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markSingle(List<Map<String, Object>> answer, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        if (Objects.nonNull(answer.get(0))) {
            for (String key : locs.keySet()) {
                if (answer.get(0).containsKey("open")) {
                    // 重置open
                    if (answer.get(0).get("open") instanceof List) {
                        List<Map<String, Object>> openList = (List) answer.get(0).get("open");
                        if (openList.size() > 0) {
                            if (openList.get(0).get("v") instanceof String) {
                                String v = (String) openList.get(0).get("v");
                                answer.get(0).put("open", markWord(v, locs.get(key)));
                                mark = true;
                            }
                        }
                    } else if (answer.get(0).get("open") instanceof String) {
                        String v = (String) answer.get(0).get("open");
                        answer.get(0).put("open", markWord(v, locs.get(key)));
                        mark = true;
                    }
                } else if ("#1".equals(key)) {
                    if (answer.get(0).get("open") instanceof List) {
                        List<Map<String, Object>> openList = (List) answer.get(0).get("open");
                        if (openList.size() > 0) {
                            if (openList.get(0).get("v") instanceof String) {
                                String v = (String) openList.get(0).get("v");
                                answer.get(0).put("open", markWord(v, locs.get(key)));
                                mark = true;
                            }
                        }
                    } else if (answer.get(0).get("open") instanceof String) {
                        String v = (String) answer.get(0).get("open");
                        answer.get(0).put("open", markWord(v, locs.get(key)));
                        mark = true;
                    }
                } else {
                    log.error("markSingle {} --> {}", key, locs.get(key));
                }
            }
        }
        return mark;
    }

    /**
     * 标记多选
     *
     * @param answer
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markMultiple(List<Map<String, Object>> answer, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        for (String key : locs.keySet()) {
            try {
                if (!key.startsWith("#")) {
                    if (answer.get(0).get("open") instanceof List) {
                        List<Object> openList = (List) answer.get(0).get("open");
                        if (openList.size() > 0) {
                            if (openList.get(0) instanceof Map) {
                                Map<String, Object> openMap = (Map<String, Object>) openList.get(0);
                                if (openMap.get("v") instanceof String) {
                                    String v = (String) openMap.get("v");
                                    answer.get(0).put("open", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            } else if (openList.get(0) instanceof String) {
                                String v = (String) openList.get(0);
                                answer.get(0).put("open", markWord(v, locs.get(key)));
                                mark = true;
                            }
                        }
                    } else if (answer.get(0).get("open") instanceof String) {
                        String v = (String) answer.get(0).get("open");
                        answer.get(0).put("open", markWord(v, locs.get(key)));
                        mark = true;
                    }
                } else {
                    if (key.contains("#")) {
                        String[] keys = key.split("#");
                        if (keys.length >= 3) {
                            Integer idx = Integer.valueOf(keys[1]);
                            if ("1".equals(keys[2])) {
                                if (answer.get(idx).get("open") instanceof List) {
                                    List<Map<String, Object>> openList = (List) answer.get(0).get("open");
                                    if (openList.size() > 0) {
                                        if (openList.get(idx).get("v") instanceof String) {
                                            String v = (String) openList.get(0).get("v");
                                            answer.get(idx).put("open", markWord(v, locs.get(key)));
                                            mark = true;
                                        }
                                    }
                                } else if (answer.get(idx).get("open") instanceof String) {
                                    String v = (String) answer.get(idx).get("open");
                                    answer.get(idx).put("open", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            } else {
                                log.error("markMultiple {} --> {}", key, locs.get(key));
                            }
                        }
                    }
                }
            } catch (Exception e) {
                log.error("markMultiple error {}", e);
            }
        }
        return mark;
    }

    /**
     * 得分处理
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markScore(List<Map<String, Object>> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        Map<String, Object> seqAnsMap = new HashMap<>();
        if (Objects.nonNull(answers)) {
            for (Map<String, Object> answer : answers) {
                seqAnsMap.put((String) answer.get("seq"), answer);
            }
        }
        for (String key : locs.keySet()) {
            try {
                if (key.contains("#")) {
                    String[] keys = key.split("#");
                    if (keys.length >= 2) {
                        if ("1".equals(keys[1])) {
                            Integer seq = Integer.valueOf(keys[0]);
                            if (seqAnsMap.get(seq) instanceof Map) {
                                Map<String, Object> ans = (Map<String, Object>) seqAnsMap.get(seq);
                                if (ans.containsKey("open")) {
                                    if (ans.get("open") instanceof List) {
                                        List<Map<String, Object>> openList = (List) ans.get("open");
                                        if (openList.size() > 0) {
                                            if (openList.get(0).get("v") instanceof String) {
                                                String v = (String) openList.get(0).get("v");
                                                ans.put("open", markWord(v, locs.get(key)));
                                                mark = true;
                                            }
                                        }
                                    } else if (ans.get("open") instanceof String) {
                                        String v = (String) ans.get("open");
                                        ans.put("open", markWord(v, locs.get(key)));
                                        mark = true;
                                    }
                                }
                            }
                        } else if ("2".equals(keys[1])) {
                            Integer seq = Integer.valueOf(keys[0]);
                            if (seqAnsMap.get(seq) instanceof Map) {
                                Map<String, Object> ans = (Map<String, Object>) seqAnsMap.get(seq);
                                if (ans.containsKey("reason")) {
                                    if (ans.get("reason") instanceof List) {
                                        List<Map<String, Object>> openList = (List) ans.get("reason");
                                        if (openList.size() > 0) {
                                            if (openList.get(0).get("v") instanceof String) {
                                                String v = (String) openList.get(0).get("v");
                                                ans.put("reason", markWord(v, locs.get(key)));
                                                mark = true;
                                            }
                                        }
                                    } else if (ans.get("reason") instanceof String) {
                                        String v = (String) ans.get("reason");
                                        ans.put("reason", markWord(v, locs.get(key)));
                                        mark = true;
                                    }
                                }
                            }
                        } else {
                            log.error("markScore {} --> {}", key, locs.get(key));
                        }
                    }
                }
            } catch (Exception e) {
                log.error("markScore error {}", e);
            }
        }
        return mark;
    }

    /**
     * 矩阵单选
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markMatrixSingle(List<Map<String, Object>> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        Map<Integer, Object> seqAnsMap = new HashMap<>();
        if (Objects.nonNull(answers)) {
            for (Map<String, Object> answer : answers) {
                seqAnsMap.put((Integer) answer.get("seq"), answer);
            }
        }
        for (String key : locs.keySet()) {
            if (key.contains("#")) {
                try {
                    if (key.contains("#")) {
                        String[] keys = key.split("#");
                        if (keys.length >= 2) {
                            if (seqAnsMap.get(Integer.valueOf(keys[0])) instanceof Map) {
                                Map<String, Object> ansMap = ((Map) seqAnsMap.get(Integer.valueOf(keys[0])));
                                if (ansMap.get("answer") instanceof List) {
                                    List<Object> ansList = (List) ansMap.get("answer");
                                    if (ansList.get(0) instanceof Map) {
                                        Map<String, Object> anssMap = (Map<String, Object>) ansList.get(0);
                                        if ("1".equals(keys[1])) {
                                            if (anssMap.get("open") instanceof List) {
                                                List<Map<String, Object>> openList = (List) anssMap.get("open");
                                                if (openList.size() > 0) {
                                                    if (openList.get(0).get("v") instanceof String) {
                                                        String v = (String) openList.get(0).get("v");
                                                        anssMap.put("open", markWord(v, locs.get(key)));
                                                        mark = true;
                                                    }
                                                }
                                            } else if (anssMap.get("open") instanceof String) {
                                                String v = (String) anssMap.get("open");
                                                anssMap.put("open", markWord(v, locs.get(key)));
                                                mark = true;
                                            }
                                        } else {
                                            log.error("markMatrixSingle {} --> {}", key, locs.get(key));
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("markMatrixSingle error {}", e);
                }
            } else {
                return markMatrixBlank(answers, locs);
            }
        }
        return mark;
    }

    /**
     * 矩阵多选
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markMatrixMultiple(List<Map<String, Object>> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        Map<Integer, Object> seqAnsMap = new HashMap<>();
        if (Objects.nonNull(answers)) {
            for (Map<String, Object> answer : answers) {
                seqAnsMap.put((Integer) answer.get("seq"), answer);
            }
        }
        for (String key : locs.keySet()) {
            if (key.contains("#")) {
                try {
                    String[] keys = key.split("#");
                    if (keys.length >= 2) {
                        if (seqAnsMap.get(keys[0]) instanceof Map) {
                            Map<String, Object> ansMap = ((Map) seqAnsMap.get(keys[0]));
                            if (ansMap.get("answer") instanceof List) {
                                List<Object> ansList = (List) ansMap.get("answer");
                                if (ansList.get(1) instanceof Map) {
                                    Map<String, Object> anssMap = (Map<String, Object>) ansList.get(1);
                                    if ("1".equals(keys[2])) {
                                        if (anssMap.get("open") instanceof List) {
                                            List<Map<String, Object>> openList = (List) anssMap.get("open");
                                            if (openList.size() > 0) {
                                                if (openList.get(0).get("v") instanceof String) {
                                                    String v = (String) openList.get(0).get("v");
                                                    anssMap.put("open", markWord(v, locs.get(key)));
                                                    mark = true;
                                                }
                                            }
                                        } else if (anssMap.get("open") instanceof String) {
                                            String v = (String) anssMap.get("open");
                                            anssMap.put("open", markWord(v, locs.get(key)));
                                            mark = true;
                                        }
                                    } else {
                                        log.error("markMatrixMultiple {} --> {}", key, locs.get(key));
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    log.error("markMatrixMultiple error {}", e);
                }
            } else {
                return markMatrixBlank(answers, locs);
            }
        }
        return mark;
    }

    /**
     * 城市标记
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public static void markCity(List<Object> answers, Map<String, List<Integer[]>> locs) {
        for (String key : locs.keySet()) {
            // 街道信息
            if ("4".equals(key) && answers.size() >= 3) {
                //answers.add(3, markWord(answers.get(3), locs.get(key)));
            }
        }
    }

    public static boolean markMultiblank1(List<Map<String, Object>> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        Map<Integer, Object> seqAnsMap = new HashMap<>();
        if (Objects.nonNull(answers)) {
            for (Map<String, Object> answer : answers) {
                seqAnsMap.put((Integer) answer.get("seq"), answer);
            }
        }
        for (String key : locs.keySet()) {
            try {
                String[] keys;
                Integer idx = 0;
                if (key.contains("#")) {
                    keys = key.split("#");
                    if (keys.length > 2) {
                        idx = Integer.valueOf(keys[1]);
                        key = String.valueOf(keys[0]);
                    }
                }
                if (seqAnsMap.get(Integer.valueOf(key)) instanceof Map) {
                    Map<String, Object> ans = (Map<String, Object>) seqAnsMap.get(Integer.valueOf(key));
                    // open类型处理
                    if (Integer.valueOf(0).equals(idx) && ans.containsKey("open")) {
                        if (ans.get("open") instanceof List) {
                            List<Object> openList = (List) ans.get("open");
                            if (openList.size() > 0) {
                                if (openList.get(0) instanceof String) {
                                    String v = (String) openList.get(0);
                                    ans.put("open", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                                if (openList.get(0) instanceof Map) {
                                    Map<String, String> vMap = (Map<String, String>) openList.get(0);
                                    String v = vMap.get("v");
                                    ans.put("open", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            }
                        } else if (ans.get("open") instanceof String) {
                            String v = (String) ans.get("open");
                            ans.put("open", markWord(v, locs.get(key)));
                            mark = true;
                        }
                    }
                    // blank类型处理
                    if (Integer.valueOf(0).equals(idx) && ans.containsKey("blank")) {
                        if (ans.get("blank") instanceof List) {
                            List<Object> openList = (List) ans.get("blank");
                            if (openList.size() > 0) {
                                if (openList.get(0) instanceof String) {
                                    String v = (String) openList.get(0);
                                    ans.put("blank", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            }
                        } else if (ans.get("blank") instanceof String) {
                            String v = (String) ans.get("blank");
                            ans.put("blank", markWord(v, locs.get(key)));
                            mark = true;
                        }
                    } else if (Integer.valueOf(1).equals(idx)) {
                        if (ans.get("open") instanceof List) {
                            List<Object> openList = (List) ans.get("open");
                            if (openList.size() > 0) {
                                if (openList.get(0) instanceof String) {
                                    String v = (String) openList.get(0);
                                    ans.put("open", markWord(v, locs.get(key)));
                                    mark = true;
                                }
                            }
                        } else if (ans.get("open") instanceof String) {
                            String v = (String) ans.get("open");
                            ans.put("open", markWord(v, locs.get(key)));
                            mark = true;
                        }
                    } else {
                        log.error("markMultiblank1 {} --> {}", key, locs.get(key));
                    }
                }
            } catch (Exception e) {
                log.error("markMultiblank1 error {}", e);
            }
        }
        return mark;
    }

    /**
     * 矩阵空白
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markMatrixBlank(List<Map<String, Object>> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        if (Objects.nonNull(answers)) {
            for (Map<String, Object> row : answers) {
                Integer rid = (Integer) row.get("gid");
                if (row.get("answer") instanceof List) {
                    List<Map<String, Object>> rowAns = (List<Map<String, Object>>) row.get("answer");
                    if (Objects.nonNull(rowAns)) {
                        Map<String, Object> rowMap = rowAns.get(0);
                        if (Objects.nonNull(rowMap)) {
                            Integer seq = (Integer) rowMap.get("seq");
                            if (Objects.nonNull(locs)) {
                                for (String key : locs.keySet()) {
                                   /* if (mark) {
                                        return true;
                                    }*/
                                    if (StringUtils.isNotEmpty(key) && key.contains(",")) {
                                        String[] keys = key.split(",");
                                        if (keys.length == 2) {
                                            // 矩阵gid和行id相等
                                            if (String.valueOf(rid).equals(keys[0]) && String.valueOf(seq).equals(keys[1])) {
                                                locs.put(keys[1], locs.get(key));
                                                locs.remove(key);
                                                mark = markMultiblank1(rowAns, locs);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return mark;
    }

    /**
     * 评估打开
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markEvaluationOpen(Map<String, Object> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        for (String key : locs.keySet()) {
            if (answers.get("open") instanceof List) {
                List<Map<String, Object>> openList = (List) answers.get("open");
                if (openList.size() > 0) {
                    if (openList.get(0).get("v") instanceof String) {
                        String v = (String) openList.get(0).get("v");
                        answers.put("open", markWord(v, locs.get(key)));
                        mark = true;
                    }
                }
            } else if (answers.get("open") instanceof String) {
                String v = (String) answers.get("open");
                answers.put("open", markWord(v, locs.get(key)));
                mark = true;
            }
        }
        return mark;
    }

    /**
     * 标记排序
     *
     * @param answers
     * @param locs
     * @return void
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public boolean markSort(List<Map<String, Object>> answers, Map<String, List<Integer[]>> locs) {
        boolean mark = false;
        Map<String, Object> seqAnsMap = new HashMap<>();
        if (Objects.nonNull(answers)) {
            for (Map<String, Object> answer : answers) {
                seqAnsMap.put(String.valueOf(answer.get("seq")), answer);
            }
        }
        for (String key : locs.keySet()) {
            try {
                if (key.contains("#")) {
                    String[] keys = key.split("#");
                    if (seqAnsMap.get(keys[0]) instanceof Map) {
                        Map<String, Object> map = (Map<String, Object>) seqAnsMap.get(keys[0]);
                        if (map.get("sort") instanceof List) {
                            List<Object> openList = (List<Object>) map.get("sort");
                            if (!CollectionUtils.isEmpty(openList) && openList.size() > 1) {
                                if (Objects.nonNull(openList.get(1))) {
                                    String v = String.valueOf(openList.get(1));
                                    openList.remove(1);
                                    openList.add(markWord1(v, locs.get(key)));
                                    mark = true;
                                }
                            }
                        } else if (map.get("sort") instanceof String) {
                            String v = (String) map.get("sort");
                            map.put("sort", markWord1(v, locs.get(key)));
                            mark = true;
                        }
                    }
                }
            } catch (Exception e) {
                log.error("markSort error {}", e);
            }
        }
        return mark;
    }

    public static List<Object> markWord1(String blank, List<Integer[]> locs) {
        List<Object> result = new ArrayList<>();
        Integer send = blank.length();
        Integer lastPos = 0;
        if (Objects.nonNull(locs) && locs.size() > 0) {
            for (Integer[] loc : locs) {
                Integer start = loc[0];
                Integer end = start + loc[1];
                if (lastPos < start) {
                    Map<String, Object> v = new HashMap<>();
                    v.put("v", blank.substring(lastPos, start) + 1);
                    result.add(v);
                }
                Map<String, Object> v = new HashMap<>();
                v.put("v", blank.substring(start, end + 1));
                v.put("marker", true);
                result.add(v);
                lastPos = end;
            }
        }
        if (lastPos < send) {
            Map<String, Object> v = new HashMap<>();
            v.put("v", blank.substring(lastPos));
        }
        return result;
    }

    /**
     * 打标记
     *
     * @param blank
     * @param locs
     * @return List<Map < String, Object>>
     * @version v1.0
     * @author gy.liu
     * @date 2021/8/27
     */
    public static List<Map<String, Object>> markWord(String blank, List<Integer[]> locs) {
        List<Map<String, Object>> result = new ArrayList<>();
        int lastPos = 0;
        Integer strEnd = blank.length();
        if (Objects.nonNull(locs) && locs.size() > 0) {
            for (Integer[] loc : locs) {
                Integer start = loc[0];
                Integer end = start + loc[1];
                if (lastPos < start) {
                    Map<String, Object> v = new HashMap<>();
                    v.put("v", blank.substring(lastPos, start));
                    result.add(v);
                }
                Map<String, Object> v = new HashMap<>();
                v.put("v", blank.substring(start, end));
                v.put("marker", true);
                result.add(v);
                lastPos = end;
            }
        }
        if (lastPos < strEnd) {
            Map<String, Object> v = new HashMap<>();
            v.put("v", blank.substring(lastPos));
            result.add(v);
        }
        return result;
    }

    public void mergeWordRange(Map<String, List<Integer[]>> wordRange1, Map<String, Object> wordRange2, String type) {
        String keyNew = "";
        if (Objects.nonNull(wordRange2)) {
            // 单选题 多选题
            List<Integer> valNew;
            String key1 = "";
            String key2 = "";
            List<Integer[]> listNew = new ArrayList<>();
            for (String key : wordRange2.keySet()) {
                List<Integer[]> list = new ArrayList<>();
                if (wordRange2.get(key) instanceof List) {
                    List<Object> val21 = (List<Object>) wordRange2.get(key);
                    if (val21.size() > 0) {
                        for (Object val : val21) {
                            if (val instanceof List) {
                                List<Integer> inta = (List<Integer>) val;
                                if (!CollectionUtils.isEmpty(inta)) {
                                    list.add(inta.toArray(new Integer[0]));
                                    listNew.add(inta.toArray(new Integer[0]));
                                }
                            }
                        }
                    }
                    // 选中处理
                    if (key.contains("#")) {
                        String[] keys = key.split("#");
                        if (keys.length == 2) {
                            key1 = keys[0];
                        }
                    }
                }

                // 矩阵填空题 矩阵单选题
                if (wordRange2.get(key) instanceof Map) {
                    Map<String, Object> range2Map = (Map<String, Object>) wordRange2.get(key);
                    // 选中的处理
                    if (range2Map.size() == 0) {
                        key2 = key;
                    }
                    if (Objects.nonNull(range2Map) && range2Map.size() > 0) {
                        for (Object ranVal : range2Map.keySet()) {
                            if (range2Map.get(String.valueOf(ranVal)) instanceof List) {
                                valNew = (List<Integer>) ((List) range2Map.get(ranVal)).get(0);
                                list.add(valNew.toArray(new Integer[0]));
                                if ("matrix_multiple".equals(type) || "matrix_blank".equals(type) || "matrix_single".equals(type)) {
                                    keyNew = key + "," + ranVal;
                                } else {
                                    keyNew = (String) ranVal;
                                }
                            }
                        }
                    }
                }

                List<Integer[]> val2 = new ArrayList<>();
                if (wordRange1.containsKey(key)) {
                    // 对于map的处理
                    if (wordRange1.get(key) instanceof Map && wordRange2.get(key) instanceof Map) {
                        Map<String, Object> v1Dict = (Map<String, Object>) wordRange1.get(key);
                        Map<String, Object> v2Dict = (Map<String, Object>) wordRange2.get(key);
                        for (String v1 : v1Dict.keySet()) {
                            if (Objects.nonNull(v2Dict.get(v1))) {
                                if (v1Dict.get(v1) instanceof Map && v2Dict.get(v1) instanceof Map) {
                                    // mergeWordRange(val1, (Map<String,Object>)wordRange2.get(key)); 需要确认格式
                                }
                            }
                        }
                    }
                    // 对于字符串的处理
                    List<Integer[]> val1 = new ArrayList<>();
                    if (wordRange1.get(key) instanceof List) {
                        //val1 = wordRange1.get(key);
                    }
                    List<Integer[]> sortVal = new ArrayList<>();
                    int idx2 = 0;
                    int val2Len = val2.size();
                    // 以危险词中心位置作为key进行归并排序
                    if (Objects.nonNull(val1)) {
                        for (Integer[] v1 : val1) {
                            int k1 = v1[0] * 2 + v1[1];
                            while (idx2 < val2Len) {
                                Integer[] v2 = val2.get(idx2);
                                int k2 = v2[0] * 2 + v2[1];
                                if (k2 < k1) {
                                    idx2 += 1;
                                    sortVal.add(v2);
                                } else {
                                    break;
                                }
                            }
                            sortVal.add(v1);
                        }
                    }
                    // 把val2列表都加入sort
                    for (int i = idx2; i < val2.size(); i++) {
                        sortVal.add(val2.get(idx2));
                    }
                    Integer[] prevVal = sortVal.get(0);
                    List<Integer[]> mergeVal = new ArrayList<>();
                    for (int i = 1; i < sortVal.size(); i++) {
                        Integer[] val = sortVal.get(i);
                        int start = 0;
                        int end = prevVal.length;
                        if (end <= val[0]) {
                            // 不需要合并
                            mergeVal.add(prevVal);
                            prevVal = val;
                        } else {
                            start = Integer.min(prevVal[0], val[0]);
                            end = Integer.max(end, val[0] + val[1]);
                            prevVal = new Integer[]{start, end - start};
                        }
                        mergeVal.add(prevVal);
                        //wordRange1.put(key, mergeVal);
                    }
                } else {
                    if (StringUtils.isNotEmpty(keyNew)) {
                        wordRange1.put(keyNew, list);
                    } else {
                        wordRange1.put(key, list);
                    }
                }
            }
            if (!StringUtils.isEmpty(key1) && !StringUtils.isEmpty(key2)) {
                wordRange1.clear();
                wordRange1.put(key2 + "," + key1, listNew);
            }
        }
    }

}
